home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / os2tools / bnklysrc / cdog.c < prev    next >
C/C++ Source or Header  |  1989-03-31  |  16KB  |  564 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software <no-Inc>                   */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          No-Cost<no-tm> Software.                       */
  7. /*        \ 1011 /                                                          */
  8. /*         ------                                                           */
  9. /*                                                                          */
  10. /*  Copyright (C) 1987, 1988, 1989 by Robert Hartman and Vincent Perriello  */
  11. /*                                                                          */
  12. /*                                                                          */
  13. /*                 This module was written by Bob Hartman                   */
  14. /*                                                                          */
  15. /*                BinkleyTerm SEAdog Mail Session Routines                  */
  16. /*                                                                          */
  17. /*                                                                          */
  18. /*    For complete  details  of the licensing restrictions, please refer    */
  19. /*    to the License  agreement,  which  is published in its entirety in    */
  20. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.210.    */
  21. /*                                                                          */
  22. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  23. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  24. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  25. /*    NOT HAVE THESE FILES,  YOU SHOULD  IMMEDIATELY CONTACT THE AUTHORS    */
  26. /*    AT THE  ADDRESSES LISTED BELOW.  IN NO EVENT SHOULD YOU PROCEED TO    */
  27. /*    USE   THIS  FILE  WITHOUT  HAVING   ACCEPTED  THE  TERMS  OF   THE    */
  28. /*    BINKLEYTERM  LICENSING AGREEMENT,  OR SUCH OTHER  AGREEMENT AS YOU    */
  29. /*    ARE ABLE TO REACH WITH THE AUTHORS.                                   */
  30. /*                                                                          */
  31. /*                                                                          */
  32. /*    The Authors can be reached at the following addresses:                */
  33. /*                                                                          */
  34. /*    Robert C. Hartman                      Vincent E. Perriello           */
  35. /*    Spark Software                         VEP Software                   */
  36. /*    427-3 Amherst Street                   111 Carroll Street             */
  37. /*    CS2032, Suite 232                      Naugatuck, CT 06770            */
  38. /*    Nashua, NH 03061                                                      */
  39. /*                                                                          */
  40. /*    FidoNet 1:132/101                      FidoNet 1:141/491              */
  41. /*    Data    (603) 888-8179                 Data    (203) 729-7569         */
  42. /*                                                                          */
  43. /*    Please feel free to contact us at any time to share your comments     */
  44. /*    about our software and/or licensing policies.                         */
  45. /*                                                                          */
  46. /*--------------------------------------------------------------------------*/
  47.  
  48. #include <signal.h>
  49. #include <ctype.h>
  50. #include <conio.h>
  51. #include <time.h>
  52. #include <string.h>
  53. #include <stdlib.h>
  54.  
  55. #define WAZOO_SECTION
  56. #define MATRIX_SECTION
  57.  
  58. #include "com.h"
  59. #include "xfer.h"
  60. #include "zmodem.h"
  61. #include "keybd.h"
  62. #include "sbuf.h"
  63. #include "sched.h"
  64. #include "externs.h"
  65. #include "prototyp.h"
  66.  
  67. #define rb_plus "rb+"
  68.  
  69. static int req_out (char *, char *, char *);
  70. static int get_req_str (char *);
  71. static void gen_req_name (char *);
  72. static int cdog_callback(char *);
  73.  
  74. static int need_to_send_ACK;
  75.  
  76. void SEA_sendreq ()
  77. {
  78.    char fname[80];
  79.    char reqf[80];
  80.    char *reqtime = " 0";
  81.    char *p, *name, *pw;
  82.    char *updtime;
  83.    int i, j, done, done1, nfiles;
  84.    FILE *fp;
  85.    long t1;
  86.    char *HoldName;
  87.  
  88.  
  89.    t1 = timerset (1000);
  90.  
  91.    HoldName = HoldAreaNameMunge(called_zone);
  92.    sprintf (fname,
  93.             "%s%04x%04x.REQ",
  94.             HoldName, called_net, called_node);
  95.    errno = 0;
  96.  
  97.    /* If we have file requests, then do them */
  98.    if (!dexists (fname))
  99.       {
  100.       status_line (":No outgoing file requests");
  101.       }
  102.    else
  103.       {
  104.       status_line (":Outbound file requests");
  105.       /* Open the .REQ file */
  106.       if ((fp = fopen (fname, "r")) == NULL)
  107.          {
  108.          SENDBYTE (ETB);
  109.      return;
  110.          }
  111.  
  112.       /* As long as we do not have EOF, read the request */
  113.       while ((fgets (reqf, 79, fp) != NULL) && (CARRIER))
  114.          {
  115.          /* Properly format the request */
  116.  
  117.          /* First get rid of the trailing junk */
  118.  
  119.          p = reqf + strlen (reqf) - 1;
  120.          while ((p >= reqf) && (isspace (*p)))
  121.             *p-- = '\0';
  122.  
  123.          /* Now get rid of the beginning junk */
  124.  
  125.          p = reqf;
  126.          while ((*p) && (isspace (*p)))
  127.             p++;
  128.  
  129.          /* This is where the name starts */
  130.          name = p;
  131.  
  132.          /* If the first char is ; then ignore the line */
  133.          if (*name == ';')
  134.             continue;
  135.  
  136.          /* Now get to where the name ends */
  137.          while ((*p) && (!isspace (*p)))
  138.             p++;
  139.  
  140.          updtime = reqtime;             /* Default to request        */
  141.          pw = p;                        /* This is or will be a null */
  142. req_scan:
  143.          if (*p)
  144.             {
  145.             *p++ = '\0';
  146.  
  147.             while ((*p) && (*p != '!') && (*p != '-') && (*p != '+'))
  148.                p++;
  149.  
  150.             /* Check for a password */
  151.  
  152.             if (*p == '!')
  153.                {
  154.                *p = ' ';
  155.                pw = p++;
  156.  
  157.                /* Now get to where the password ends */
  158.  
  159.                while ((*p) && (!isspace (*p)))
  160.                   p++;
  161.                goto req_scan;
  162.                }
  163.  
  164.             /* Try for an update request */
  165.  
  166.             if (*p == '+')
  167.                {
  168.                *p = ' ';
  169.                updtime = p++;
  170.  
  171.                /* Now get to where the update time ends */
  172.  
  173.                while ((*p) && (!isspace (*p)))
  174.                   p++;
  175.  
  176.                *p = '\0';
  177.  
  178.                /* Request time is last thing we care about on line, so
  179.                   we just drop out of the loop after finding it.
  180.                 */
  181.                }
  182.  
  183.             /* Note: since SEAdog doesn't do "backdate" requests, all we
  184.                have to do if we see a '-' is to do a full request. Hence
  185.                we only process a '+' here.
  186.              */
  187.  
  188.             }
  189.  
  190.          if (req_out (name, pw, updtime))
  191.             continue;
  192.  
  193.          /* Wait for ACK or ENQ */
  194.          t1 = timerset (6000);
  195.          done = 0;
  196.          while ((!timeup (t1)) && CARRIER && !done)
  197.             {
  198.             j = TIMED_READ (0);
  199.             if (j >= 0)
  200.                {
  201.                if (j == ACK)
  202.                   {
  203.                   /* If ACK, receive files using SEAlink */
  204.                   nfiles = 0;
  205.                   done1 = 0;
  206.                   do
  207.                      {
  208.                      if ((i = try_sealink ()) == 0)
  209.                         {
  210.                         if (!recvmdm7 (reqf))
  211.                            {
  212.                            done1 = 1;
  213.                            }
  214.                         else
  215.                            {
  216.                            if (!receive_file (CurrentNetFiles, NULL, 'T'))
  217.                               {
  218.                               if (locate_y > 1 && !(fullscreen && un_attended))
  219.                                  gotoxy (0, locate_y - 1);
  220.                               done1 = 1;
  221.                               }
  222.                            else ++nfiles;
  223.                            }
  224.                         }
  225.                      else if (i == 1)
  226.                         {
  227.                         if (!receive_file (CurrentNetFiles, NULL, 'F'))
  228.                            {
  229.                            if (locate_y > 1 && !(fullscreen && un_attended))
  230.                               gotoxy (0, locate_y - 1);
  231.                            done1 = 1;
  232.                            }
  233.                         else ++nfiles;
  234.                         }
  235.                      else
  236.                         {
  237.                         done1 = 1;
  238.                         }
  239.                      } while (CARRIER && !done1);
  240.  
  241.                   status_line (":Received %d files", nfiles);
  242.                   done = 1;
  243.                   t1 = timerset (6000);
  244.  
  245.                   /* wait for request start */
  246.                   while ((TIMED_READ (0) != ENQ) && (!timeup (t1)) && CARRIER)
  247.                      time_release ();
  248.                   }
  249.                else if (j == ENQ)
  250.                   {
  251.                   /* If ENQ, report no files received */
  252.                   req_out (name, pw, updtime);
  253.                   }
  254.                else
  255.                   {
  256.                   time_release ();
  257.                   if (!j)
  258.                      t1 = timerset (6000);
  259.                   }
  260.                }
  261.             }
  262.          }
  263.       fclose (fp);
  264.       unlink (fname);
  265.       status_line (":End of outbound file requests");
  266.       }
  267.  
  268.    /* Finish the file requests off */
  269.    SENDBYTE (ETB);
  270. }
  271.  
  272. int SEA_recvreq ()
  273. {
  274.    int j;
  275.    int done = 0;
  276.    int nfiles = 0;
  277.    int nfiles1;
  278.    char req[64];
  279.    long t1, timerset ();
  280.    int n_frproc (char *, int, int (*)(char *));
  281.  
  282.    t1 = timerset (2000);
  283.  
  284.    if (no_requests)
  285.       {
  286.       /* Refuse file requests */
  287.       SENDBYTE (CAN);
  288.       status_line ("*Refusing inbound file requests");
  289.       return TRUE;
  290.       }
  291.  
  292.    /* Try the bark stuff */
  293.    status_line (":Inbound file requests");
  294.    while (CARRIER && !done && (!timeup (t1)))
  295.       {
  296.       /* Send out the start signal */
  297.       SENDBYTE (ENQ);
  298.  
  299.       /* Wait for the response */
  300.       j = TIMED_READ (2);
  301.  
  302.       switch (j)
  303.          {
  304.          case ACK:
  305.             nfiles1 = nfiles;
  306.             if (get_req_str (req))
  307.                {
  308.                gen_req_name (req);
  309.                need_to_send_ACK = 1;
  310.                if (((nfiles = n_frproc (req, nfiles, cdog_callback)) < 0)
  311.                || nfiles1 == nfiles)
  312.                   {
  313.                   SENDBYTE (ACK);
  314.                   send_file (NULL, 'S');
  315.                   }
  316.                else
  317.                   status_line (":%d matching files sent", nfiles-nfiles1);
  318.                }
  319.             else
  320.                send_file (NULL, 'S');
  321.  
  322.             if (nfiles < 0)             /* Error returned from n_frproc */
  323.                done = 1;                /* Should end request           */
  324.             t1 = timerset (2000);
  325.             break;
  326.  
  327.          case ETB:
  328.          case ENQ:
  329.             done = 1;
  330.             break;
  331.  
  332.          case 'C':
  333.          case NAK:
  334.             SENDBYTE (EOT);
  335.             CLEAR_INBOUND ();
  336.             break;
  337.          }
  338.       }
  339.    status_line (":End of inbound file requests");
  340.    return TRUE;
  341. }
  342.  
  343. int try_sealink ()
  344. {
  345.    int i, j;
  346.    long t1, timerset ();
  347.  
  348.    CLEAR_INBOUND ();
  349.  
  350.    for (i = 0; i < 5; i++)
  351.       {
  352.       SENDBYTE ('C');
  353.  
  354.       t1 = timerset (100);
  355.       while (!timeup (t1) && CARRIER)
  356.          {
  357.          if ((j = PEEKBYTE ()) >= 0)
  358.             {
  359.             if ((j == SOH) || (j == SYN))
  360.                return (1);
  361.             j = TIMED_READ (0);
  362.             if (j == EOT)
  363.                return (2);
  364.             else if (j == TSYNC)
  365.                return (0);
  366.             }
  367.          else
  368.             {
  369.             time_release ();
  370.             }
  371.          }
  372.  
  373.       if (!CARRIER)
  374.          break;
  375.       }
  376.  
  377.    return (0);
  378. }
  379.  
  380. static int req_out (name, pw, updtime)
  381. char *name, *pw, *updtime;
  382. {
  383.    char *p;
  384.    unsigned int crc;
  385.  
  386.    /* send the request */
  387.    p = name;
  388.    if (!*p)
  389.       return (1);
  390.  
  391.    status_line ("*Requesting '%s' %s%s", name, (*pw) ? "with password" : "", pw);
  392.    SENDBYTE (ACK);
  393.    crc = 0;
  394.    while (*p)
  395.       {
  396.       SENDBYTE (*p);
  397.       crc = xcrc (crc, (byte) (*p));
  398.       ++p;
  399.       }
  400.  
  401.    /* Now send the update time */
  402.  
  403.    p = updtime;
  404.    while (*p)
  405.       {
  406.       SENDBYTE (*p);
  407.       crc = xcrc (crc, (byte) (*p));
  408.       ++p;
  409.       }
  410.  
  411.    /* Finally the password     */
  412.  
  413.    p = pw;
  414.    while (*p)
  415.       {
  416.       SENDBYTE (*p);
  417.       crc = xcrc (crc, (byte) (*p));
  418.       ++p;
  419.       }
  420.  
  421.    SENDBYTE (ETX);
  422.    SENDBYTE ((unsigned char) (crc & 0xff));
  423.    SENDBYTE ((unsigned char) (crc >> 8));
  424.    return (0);
  425. }
  426.  
  427. static int get_req_str (req)
  428. char *req;
  429. {
  430.    unsigned int crc, crc1, crc2, crc3;
  431.    int i, j;
  432.  
  433.    crc = i = 0;
  434.    while (CARRIER)
  435.       {
  436.       j = TIMED_READ (2);
  437.       if (j < 0)
  438.          return (0);
  439.  
  440.       if (j == ETX)
  441.          {
  442.          crc1 = TIMED_READ (2);
  443.          crc2 = TIMED_READ (2);
  444.          crc3 = (crc2 << 8) + crc1;
  445.          if (crc3 != crc)
  446.             {
  447.             status_line ("!Bad crc - trying again");
  448.             return (0);
  449.             }
  450.          req[i] = '\0';
  451.          SENDBYTE (NUL);
  452.          return (1);
  453.          }
  454.       else
  455.          {
  456.          req[i++] = (char) (j & 0xff);
  457.      crc = xcrc (crc, (j & 0xff));
  458.          }
  459.       }
  460.    return (0);
  461. }
  462.  
  463. /*
  464.  * gen_req_name -- take the name [time] [password] fields from
  465.  *                 the BARK file request format and reformat to
  466.  *                 name [!password] [+time] WaZOO format for use
  467.  *                 by the WaZOO file request routines.
  468.  *
  469.  * Input:          *req = pointer to character array with Bark string
  470.  * Output:         *req array contents reformatted
  471.  *
  472.  */
  473.  
  474.  
  475. static void gen_req_name (req)
  476. char *req;
  477. {
  478.    char *q, *q1;
  479.    char buf[48];
  480.    char *fsecs = NULL;
  481.  
  482.    q = req;
  483.    q1 = buf;
  484.  
  485.    /* Get the filename */
  486.  
  487.    while ((*q) && (!isspace (*q)))
  488.       {
  489.       *q1++ = *q++;
  490.       }
  491.    *q1 = '\0';
  492.  
  493.    /* If we have more characters, go on */
  494.  
  495.    if (*q)
  496.       {
  497.       /* Skip the space */
  498.       fsecs = q++;
  499.       *fsecs = '+';
  500.  
  501.       /* Skip the digits */
  502.  
  503.       while ((*q) && (!isspace (*q)))
  504.          q++;
  505.  
  506.       /* If we have more, get the password */
  507.  
  508.       if (*q)
  509.          {
  510.          *q++ = '\0';                   /* Skip space, terminate the time */
  511.  
  512.          *q1++ = ' ';
  513.          *q1++ = '!';
  514.          while (*q)
  515.             {
  516.             *q1++ = *q++;
  517.             }
  518.          *q1 = '\0';
  519.          }
  520.  
  521.       /* If we got an update time          */
  522.       if (fsecs != NULL)
  523.          {
  524.          *q1++ = ' ';
  525.          while (*fsecs)
  526.             {
  527.             *q1++ = *fsecs++;
  528.             }
  529.          *q1 = '\0';
  530.          }
  531.       }
  532.  
  533.    strcpy (req, buf);
  534.    return;
  535. }
  536.  
  537. static int cdog_callback(reqs)
  538. char *reqs;
  539. {
  540.    int i;
  541.    char p;
  542.  
  543.    if (need_to_send_ACK)
  544.       {
  545.       SENDBYTE (ACK);
  546.       need_to_send_ACK = 0;
  547.       }
  548.    if (reqs[0])
  549.       i = xfermdm7 (reqs);
  550.    else i = 2;
  551.  
  552.    p = 'T';
  553.    if (i == 0)
  554.       {
  555.       net_problems = 1;
  556.       return (0);
  557.       }
  558.    else if (i == 2)
  559.       p = 'F';
  560.  
  561.    send_file (reqs, p);
  562.    return (1);
  563. }
  564.